package Views;

import Controllers.CustomerCTR;
import Models.Customer;
import Views.TableModels.CustomerTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Tanja og Morten
 */
public class CustomerPicker extends javax.swing.JFrame implements ListSelectionListener
{
	private Customer chosenCustomer;
	private CustomerCTR controller = new CustomerCTR();

    public CustomerPicker()
	{
        initComponents();
		customerTable.getSelectionModel().addListSelectionListener(this);
		customerTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(customerTable.getSelectedRowCount() == 1)
					{
						chooseButtonActionPerformed(null);
					}
				}
			}
		 });
    }

	public Customer getChosenCustomer()
	{
		return this.chosenCustomer;
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        contactLabel = new javax.swing.JLabel();
        contactTF = new javax.swing.JTextField();
        cvrLabel = new javax.swing.JLabel();
        cvrSP = new javax.swing.JSpinner();
        addressLabel = new javax.swing.JLabel();
        addressTF = new javax.swing.JTextField();
        zipCodeLabel = new javax.swing.JLabel();
        zipCodeSP = new javax.swing.JSpinner();
        cityLabel = new javax.swing.JLabel();
        cityTF = new javax.swing.JTextField();
        phoneNoLabel = new javax.swing.JLabel();
        phoneNoSP = new javax.swing.JSpinner();
        jScrollPane1 = new javax.swing.JScrollPane();
        customerTable = new javax.swing.JTable();
        chooseButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Kundevælger");

        nameLabel.setText("Navn eller firmanavn:");

        contactLabel.setText("Kontaktperson (kun firma):");

        cvrLabel.setText("CVR (kun firma):");

        addressLabel.setText("Adresse:");

        zipCodeLabel.setText("Postnummer:");

        cityLabel.setText("By:");

        phoneNoLabel.setText("Telefonnummer:");

        customerTable.setModel(new CustomerTableModel());
        jScrollPane1.setViewportView(customerTable);

        chooseButton.setText("Vælg");
        chooseButton.setEnabled(false);
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Annuller");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        searchButton.setText("Søg");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                    .add(addressLabel)
                    .add(searchButton)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(contactLabel)
                                .add(cvrLabel)
                                .add(nameLabel)
                                .add(phoneNoLabel)
                                .add(zipCodeLabel))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(zipCodeSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(cityLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(cityTF))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, cvrSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 161, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, addressTF)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, contactTF)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, nameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
                                .add(phoneNoSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 156, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(layout.createSequentialGroup()
                            .add(chooseButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(cancelButton))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(contactLabel)
                    .add(contactTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cvrLabel)
                    .add(cvrSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addressLabel)
                    .add(addressTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(zipCodeLabel)
                    .add(zipCodeSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(cityLabel)
                    .add(cityTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(phoneNoLabel)
                    .add(phoneNoSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(searchButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 171, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(chooseButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chooseButtonActionPerformed
	{//GEN-HEADEREND:event_chooseButtonActionPerformed
		int chosenId = Integer.parseInt(customerTable.getValueAt(customerTable.getSelectedRow(), 0).toString());
		this.chosenCustomer = this.controller.getCustomer(chosenId);
		this.doClose();
}//GEN-LAST:event_chooseButtonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
	{//GEN-HEADEREND:event_cancelButtonActionPerformed
		this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
		int zipCode = Integer.parseInt(zipCodeSP.getValue().toString());
		int phoneNo = Integer.parseInt(phoneNoSP.getValue().toString());
		int cvr = Integer.parseInt(cvrSP.getValue().toString());
		customerTable.setModel(
				new CustomerTableModel(
					this.controller.findCustomer(
						addressTF.getText(),
						zipCode,
						cityTF.getText(),
						phoneNo,
						nameTF.getText(),
						contactTF.getText(),
						cvr)));
}//GEN-LAST:event_searchButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(customerTable.getSelectedRowCount() == 1)
		{
			chooseButton.setEnabled(true);
		}
		else
		{
			chooseButton.setEnabled(false);
		}
	}

	public void doClose()
	{
        WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        processWindowEvent(evt);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel addressLabel;
    private javax.swing.JTextField addressTF;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JTextField cityTF;
    private javax.swing.JLabel contactLabel;
    private javax.swing.JTextField contactTF;
    private javax.swing.JTable customerTable;
    private javax.swing.JLabel cvrLabel;
    private javax.swing.JSpinner cvrSP;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JLabel phoneNoLabel;
    private javax.swing.JSpinner phoneNoSP;
    private javax.swing.JButton searchButton;
    private javax.swing.JLabel zipCodeLabel;
    private javax.swing.JSpinner zipCodeSP;
    // End of variables declaration//GEN-END:variables
}